<?php

/**
 * login actions.
 *
 * @package    sf.shonanbbs.com
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class loginActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex(){

      $this->mailaddress = $this->getRequestParameter('mailaddress');
      return sfView::SUCCESS;
  }

  public function executeSubmit()
  {
      $this->mailaddress = $this->getRequestParameter('mailaddress');
      $this->password = $this->getRequestParameter('password');

      if ($this->mailaddress != '' && $this->password != '') {
          $c = new Criteria;
          $c->add(MemberPeer::MAILADDRESS, $this->mailaddress);
          $c->add(MemberPeer::PASSWORD, $this->password);
          $member = MemberPeer::doSelectOne($c);

          //↓のロジックは上記４行を別の場所に記述して、それを読み込んでいる
          //$member = MemberPeer::retrieveByPassword($this->mailaddress, $this->password);機能を拡張している

          if($member) {

              // ログイン状態にする
              $this->getUser()->setAuthenticated(true);
              $this->getUser()->clearCredentials();
              $this->getUser()->addCredential('member');

              // 会員情報をセッションにセット
              $this->getUser()->setAttribute('member_id', $member->getId(),sfConfig::get('sf_session_name'));
              $this->getUser()->setAttribute('nickname', $member->getNickname(), sfConfig::get('sf_session_name'));


              //管理者権限をもっているなら
              if($member->getAdminYn() == "y"){
                  $this->getUser()->addCredential("admin");
              }

              $this->redirect("/backend");


          }

      }
      $this->redirect("/");
      //$this->forward('default', 'module');
  }

  //バリデーションに引っ掛かったら動く処理
  public function handleErrorSubmit(){
      $this->request->setParameter("mailaddress", $this->getRequestParameter('mailaddress'));
      $this->forward("login","index");
  }

}
